let defaultLists = JSON.parse(localStorage.getItem( 'todos' )) || []

const todos = (state = defaultLists, action) => {
    switch (action.type) {
        case 'ADD_TODO':
            return [
                ...state, {
                    id: action.id,
                    text: action.text,
                    completed: false
                }
            ]
        case 'TOGGLE_TODO':
            return state.map(todo =>
                (todo.id === action.id) ? {...todo,
                    completed: !todo.completed
                } : todo
            )
        case 'EDIT':
            console.log(action)
            return state.map(todo => {
                return (todo.id === action.id) ? 
                    {
                        ...todo,
                        editing: true
                    } : todo
            })
        case 'CLOSE_EDIT':
            return state.map(todo => {
                return (todo.id === action.id) ? 
                    {
                        ...todo,
                        editing: false
                    } : todo
            })
        case 'SAVE_EDIT':
            return state.map(todo => {
                return (todo.id === action.id) ? 
                    {
                        ...todo,
                        text: action.text
                    } : todo
            })
        case 'DEL':
            return state.filter(function(ele) {
                return ele.id !== action.id
            });
        default:
            return state
    }
}

export default todos